#include "FKButtonRoom.h"

#include "FKLiveButton.h"
#include "FKActor.h"
#include "FKUser.h"
#include "FKEventNames.h"
#include "FKActorsModel.h"

#include "FKLogger.h"
#include "FKModelRole.h"

#include "FKButtonWorldReferent.h"
#include "FKRoomInterface.h"

FK_EVENTS(FKButtonRoom){

}

FKButtonRoom::FKButtonRoom(FKRoom* room,qint32 objectID):FKRoom(room,objectID),
        _blind(false),_currentActor(0){
    FK_CBEGIN
    setModel(new FKActorsModel(this));
//    setWorld(new FKWorldReferent(this));
//    world()->setActorsModel(actorsModel());
    FK_CEND
}

FKButtonRoom::~FKButtonRoom(){
    FK_DBEGIN
//    foreach (FKObject* obj, _actors)obj->deleteFKObject();
//    foreach (FKObject* obj, _buttons)obj->deleteFKObject();
    FK_DEND
}

//void FKButtonRoom::E_loadScene(FKEvent *event){
//    Q_UNUSED(event)
//    FKWorldReferent* w=world();
//    FKDisplayReferent::setWorldReferent(w);
//    FKDrawModel* m=new FKSimpleDrawModel(FKSDM::quad,0.3f,0.2f,0.02f,FKSDM::quad, w);
//    w->addModel(m);
//    m->initModel();
//}

void FKButtonRoom::loadCore(){
    loadActors();
    for(int i=0;i<3;++i){
        FKLiveButton* b=new FKLiveButton(this);
        connect(b,SIGNAL(clicked(FKLiveButton*)),SLOT(buttonClicked(FKLiveButton*)));
//        b->setColor(QColor(Qt::gray));
        b->move(-0.5+i*0.5);
        _buttons.append(b);
        foreach(FKActor* a, _actors){
            a->setTotalWatching(b);
        }
        b->applyUpdates();
    }
    connect(_actors.at(0),SIGNAL(won(FKActor*)),_actors.at(1),SLOT(lose()));
    connect(_actors.at(1),SIGNAL(won(FKActor*)),_actors.at(0),SLOT(lose()));
}

void FKButtonRoom::loadUser(FKUser *user){
    user->setTotalWatching(getObject(actorsModel()->actorForUserID(user->getID())));
}

void FKButtonRoom::initStart(){
    _blind=false;
    _currentActor=qrand() % 2;
    qint32 nextActor=(_currentActor ? 0 : 1);
    _winConditoins.clear();
    qint32 button1=qrand() % 3;
    qint32 button2;
    do{
        button2=qrand() % 3;
    }while(button1==button2);
    FKActor* curActorRef=_actors[_currentActor];
    FKActor* nextActorRef=_actors[nextActor];
    nextActorRef->setActive(false);
    FKLiveButton* b1=_buttons[button1];
    FKLiveButton* b2=_buttons[button2];
    _winConditoins[curActorRef]=b1;
    _winConditoins[nextActorRef]=b2;
    foreach(FKLiveButton* b,_buttons){
        b->setColor(Qt::gray);
        b->setScore(0);
        b->applyUpdates();
        curActorRef->setCustomWatching(b,FKEventName::c_color);
        nextActorRef->stopCustomWatching(b,FKEventName::c_color);
        b->allowControlBy(curActorRef);
        b->allowControlBy(nextActorRef);
    }
    b1->setColor(QColor(Qt::red));
    b2->setColor(QColor(Qt::blue));
    b1->applyUpdates();
    b2->applyUpdates();
}


void FKButtonRoom::buttonClicked(FKLiveButton* b){
    b->addScore(_blind ? 2 : 1);
    b->applyUpdates();
    if(!checkWin()){
        nextTurn();
    }else{
        initStart();
    }
}

bool FKButtonRoom::checkWin(){
    foreach(FKActor* a,_actors){
        if(_winConditoins.value(a)->score()>=10){
            a->win();
            return true;
        }
    }
    return false;
}

void FKButtonRoom::nextTurn(){
    _blind=!_blind;
    qint32 nextActor=(_currentActor ? 0 : 1);
    _actors[_currentActor]->setActive(false);
    _actors[nextActor]->setActive(true);
//    foreach(FKLiveButton* lb,_buttons){
        //lb->forbidControlBy(_actors[_currentActor]);
        //lb->allowControlBy(_actors[nextActor]);
//    }
    _currentActor=nextActor;
}

bool FKButtonRoom::startCondition(){
//    return true;
    return _users.count()==2;
}

//void FKButtonRoom::userGone(FKUser *user){
//    user->stopTotalWatching();
//}

void FKButtonRoom::connectInterface(QObject *roomInterface){
    connect(this,SIGNAL(actorMessage(QString)),roomInterface,SLOT(showMessage(QString)));
}

FKWorldReferent* FKButtonRoom::worldClass()const{
    return new FKButtonWorldReferent();
}

FKRoomInterface* FKButtonRoom::interfaceClass()const{
    return new FKRoomInterface();
}
